package sort;

import java.util.Arrays;
import java.util.Date;

/**
 * Created by IntelliJ IDEA.
 * User: 杰哥
 * Date: 2020/4/5
 * Time: 17:00
 * 选择排序
 */
public class SelectSort {
    public static void main(String[] args) {
//        int arr[] = {8, 9, 1, 7, 2, 3, 5, 4, 6, 0};
        int arr[] = new int[200000];
        for (int i = 0; i < 200000; i++) {
            arr[i] = (int)(Math.random() * 200000);
        }
        Date date1 = new Date();
        selectSort(arr);
        Date date2 = new Date();
        System.out.println("用时：" + (date2.getTime() - date1.getTime()) + "毫秒" );
//        System.out.println(Arrays.toString(arr));
    }

    /**
     * 选择排序
     * @param arr
     */
    public static void selectSort(int[] arr) {
        //记录最小值
        int miniVal;
        //记录最小值所在下标
        int minIndex;
        for (int i = 0; i < arr.length - 1 ; i++) {
            miniVal = arr[i];
            minIndex = i;
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[j] < miniVal) {
                    miniVal = arr[j];
                    minIndex = j;
                }
            }
            arr[minIndex] = arr[i];
            arr[i] = miniVal;
        }
    }
}
